package cqut.wys.websocket;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * WebSocketServer服务端继承和扩展
 * @author wuyoushan@zbj.com
 * @date 2017/3/15.
 */
public class WebSocketServerExt extends WebSocketServer{

    int j=0;
    int h=0;
    int e=0;
    int l=0;

    public WebSocketServerExt(InetSocketAddress address) {
        super(address);
        System.out.println("地址" + address);
    }

    public WebSocketServerExt(int port) throws UnknownHostException {
        super(new InetSocketAddress(port));
        System.out.println("端口" + port);
    }

    /**
     * 出发连接事件
     * @param conn
     * @param handshake
     */
    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        System.out.println("有人连接Socket conn:" + conn);
        l++;
    }

    /**
     * 出发关闭事件
     * @param conn
     * @param code
     * @param reason
     * @param remote
     */
    @Override
    public void onClose(org.java_websocket.WebSocket conn, int code, String reason, boolean remote) {
        userLeave(conn);
    }

    /**
     * 客户端发送消息到服务器时出发的事件
     * @param conn
     * @param message
     */
    @Override
    public void onMessage(org.java_websocket.WebSocket conn, String message) {
        if(message != null) {
            //将用户加入
            this.userJoin(message, conn);
        }
    }

    /**
     * 触发异常事件
     * @param conn
     * @param ex
     */
    @Override
    public void onError(org.java_websocket.WebSocket conn, Exception ex) {
        System.out.println("Socket异常:" + ex.toString());
        e++;
    }

    /**
     * 用户下线处理
     * @param conn
     */
    public void userLeave(WebSocket conn) {
        String user=WebSocketPool.getUserByKey(conn);
        boolean b=WebSocketPool.removeUser(conn);   //在连接池中移除连接
        if(b){
            WebSocketPool.sendMessage(user); // 把当前用户从所有在线用户列表中删除
            String leaveMsg = "[系统]" + user + "下线了";
            WebSocketPool.sendMessage(leaveMsg); // 向在线用户发送当前用户退出的信息
        }

    }

    /**
     * 用户加入处理
     * @param user
     * @param conn
     */
    public void userJoin(String user, WebSocket conn) {
        WebSocketPool.sendMessage(user); // 把当前用户加入到所有在线用户列表中
        String joinMsg = "[系统]" + user + "上线了！";
        WebSocketPool.sendMessage(joinMsg); // 向所有在线用户推送当前用户上线的消息
        WebSocketPool.addUser(user, conn); // 向连接池添加当前的连接的对象
        WebSocketPool.sendMessageToUser(conn, WebSocketPool.getOnlineUser().toString());
        // 向当前连接发送当前在线用户的列表
    }

    public static void main(String[] args) throws InterruptedException{

//        System.out.println("开始启动webSocket");
//        WebSocketImpl.DEBUG = false;
//        int port = 8888; // 端口随便设置，只要不跟现有端口重复就可以了
//        WebSocketServerExt s =null;
//        try {
//            s = new WebSocketServerExt(port);
//            s.start();
//        } catch (UnknownHostException e1) {
//            System.out.println("启动webSocket失败！");
//            e1.printStackTrace();
//        }
//        System.out.println("启动webSocket成功！");
    }
}
